package tests.datastructures.queues;

import static org.junit.Assert.*;

import java.util.Iterator;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import datastructures.queues.LinkedQueueWithSentinelNode;
import datastructures.queues.QueueADT;

public class LinkedQueueWithSentinelNodeTestCase {

	private QueueADT<String> fixture;

	@Before
	public void setUp() throws Exception {
		this.fixture = new LinkedQueueWithSentinelNode<String>();
	}

	@After
	public void tearDown() throws Exception {
		this.fixture = null;
	}

	@Test
	public void testInitializing() {
		assertTrue(this.fixture.isEmpty());
		assertEquals(0, this.fixture.size());
	}

	@Test
	public void testEnqueue() {
		String tmpValue1 = "Gatsby";
		String tmpValue2 = "is";
		String tmpValue3 = "the";
		String tmpValue4 = "best";
		this.fixture.enqueue(tmpValue1);
		assertEquals(1, this.fixture.size());
		assertFalse(this.fixture.isEmpty());
		this.fixture.enqueue(tmpValue2);
		assertEquals(2, this.fixture.size());
		this.fixture.enqueue(tmpValue3);
		assertEquals(3, this.fixture.size());
		this.fixture.enqueue(tmpValue4);
		assertEquals(4, this.fixture.size());
	}

	@Test
	public void testFirst() {
		String tmpValue1 = "Gatsby";
		this.fixture.enqueue(tmpValue1);
		assertEquals(tmpValue1, this.fixture.first());
		String tmpValue2 = "is";
		this.fixture.enqueue(tmpValue2);
		assertEquals(tmpValue1, this.fixture.first());
		String tmpValue3 = "the";
		this.fixture.enqueue(tmpValue3);
		assertEquals(tmpValue1, this.fixture.first());
		String tmpValue4 = "best";
		this.fixture.enqueue(tmpValue4);
		assertEquals(tmpValue1, this.fixture.first());
	}

	@Test
	public void testDequeue() {
		String tmpValue1 = "Gatsby";
		String tmpValue2 = "is";
		String tmpValue3 = "the";
		String tmpValue4 = "best";
		this.fixture.enqueue(tmpValue1);
		this.fixture.enqueue(tmpValue2);
		this.fixture.enqueue(tmpValue3);
		this.fixture.enqueue(tmpValue4);
		assertFalse(this.fixture.isEmpty());
		assertEquals(4, this.fixture.size());
		assertEquals(tmpValue1, this.fixture.dequeue());
		assertEquals(tmpValue2, this.fixture.dequeue());
		assertEquals(tmpValue3, this.fixture.dequeue());
		assertEquals(tmpValue4, this.fixture.dequeue());
		assertTrue(this.fixture.isEmpty());
		assertEquals(0, this.fixture.size());
	}

	@Test
	public void testIterator() {
		String tmpValue1 = "Gatsby";
		String tmpValue2 = "is";
		String tmpValue3 = "the";
		String tmpValue4 = "best";
		this.fixture.enqueue(tmpValue1);
		this.fixture.enqueue(tmpValue2);
		this.fixture.enqueue(tmpValue3);
		this.fixture.enqueue(tmpValue4);

		String[] stringArray = new String[4];
		stringArray[0] = tmpValue1;
		stringArray[1] = tmpValue2;
		stringArray[2] = tmpValue3;
		stringArray[3] = tmpValue4;

		// shorthand
		int index = 0;
		for (String s : this.fixture) {
			assertEquals(stringArray[index++], s);
		}

		// longhand
		index = 0;
		Iterator<String> iterator = this.fixture.iterator();
		while (iterator.hasNext()) {
			String s = iterator.next();
			assertEquals(stringArray[index++], s);
		}
	}
}
